package com.example.test01;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.util.concurrent.atomic.AtomicInteger;


/**
 * Created by xha on 2015/12/28.
 */
@SuppressLint("NewApi") public class MenuItem extends LinearLayout {

    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);

    private RelativeLayout mContainer;
    private TextView mTitle;
    private TextView mSubTitle;
    private ToggleButton mToggleButton;
    private TextView mRightTextView;
    private ImageView mRightArrow;
    private ImageView mMenuIcon;
    private View mTopDivider;
    private View mBottomDivider;
    private View mItemDivider;

    private String mTitleText;
    private String mSubTitleText;
    private String mRightText;
    private Drawable mRightArrowRes;
    private int mTitleColor = 0;
    private int mSubTitleColor = 0xFF999999;
    private int mRightTextColor = 0xFF000000;
    private boolean mToggleButtonState = false;
    private Drawable mMenuToggleButtonRes = null;
    private float mMenuTitleMargin = 10;
    private float mTitleSize = 16; // unit sp
    private float mSubTitleSize = 14; // unit sp
    private float mRightTextSize = 14; // unit sp
    private int mRightTextAutoLink = 0; // unit sp
    private Drawable mMenuIconRes;
    private float mMenuIconWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
    private float mMenuIconHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
    private float mMenuIconMargin = 4; //unit dp
    private Drawable mMenuTopDivider;
    private Drawable mMenuBottomDivider;
    private float mTopDividerHeight = 1;
    private float mBottomDividerHeight = 1;
    private Drawable mMenuItemDivider;
    private float mMenuItemDividerHeight = 1;
    private Drawable mMenuRightTextDrawable;
    private float mMenuRightTextDrawablePadding;
    private float mMenuRightTextMarginLeft;
    private float mMenuRightTextMarginTop;
    private float mMenuRightTextMarginRight;
    private float mMenuRightTextMarginBottom;

    private float mContainerPaddingLeft;
    private float mContainerPaddingTop;
    private float mContainerPaddingRight;
    private float mContainerPaddingBottom;

//    private MenuItemOnClickListener mMenuItemOnClickListener;
//    private View.OnClickListener mViewOnClickListener = new View.OnClickListener() {
//        @Override
//        public void onClick(View view) {
//            if (null != mToggleButton) {
//                mToggleButtonState = !mToggleButtonState;
//                mToggleButton.setChecked(mToggleButtonState);
//            }
//            if (null != mMenuItemOnClickListener) {
//                mMenuItemOnClickListener.onClick(view);
//            }
//        }
//    };

    public MenuItem(Context context) {
        this(context, null);
    }

    public MenuItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        initFromAttributes(context, attrs);
    }

    private void initFromAttributes(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MenuItem);
        mTitleText = a.getString(R.styleable.MenuItem_menuTitle);
        mSubTitleText = a.getString(R.styleable.MenuItem_menuSubTitle);
        mRightText = a.getString(R.styleable.MenuItem_menuRightText);
        mRightTextSize = a.getDimension(R.styleable.MenuItem_menuRightTextSize, mRightTextSize);
        mRightTextAutoLink = a.getInt(R.styleable.MenuItem_menuRightTextAutoLink, mRightTextAutoLink);
        mTitleColor = a.getColor(R.styleable.MenuItem_menuTitleColor, mTitleColor);
        mSubTitleColor = a.getColor(R.styleable.MenuItem_menuSubTitleColor, mSubTitleColor);
        mRightTextColor = a.getColor(R.styleable.MenuItem_menuRightTextColor, mRightTextColor);
        mTitleSize = a.getDimension(R.styleable.MenuItem_menuTitleSize, mTitleSize);
        mSubTitleSize = a.getDimension(R.styleable.MenuItem_menuSubTitleSize, mSubTitleSize);
        mRightArrowRes = a.getDrawable(R.styleable.MenuItem_menuRightArrow);
        mToggleButtonState = a.getBoolean(R.styleable.MenuItem_menuToggleButtonState, mToggleButtonState);
        mMenuToggleButtonRes = a.getDrawable(R.styleable.MenuItem_menuToggleButton);
        mMenuTitleMargin = a.getDimension(R.styleable.MenuItem_menuTitleMargin, mMenuTitleMargin);
        mMenuIconRes = a.getDrawable(R.styleable.MenuItem_menuMenuIcon);
        mMenuIconWidth = a.getDimension(R.styleable.MenuItem_menuIconWidth, mMenuIconWidth);
        mMenuIconHeight = a.getDimension(R.styleable.MenuItem_menuIconHeight, mMenuIconHeight);
        mMenuIconMargin = a.getDimension(R.styleable.MenuItem_menuIconMargin, mMenuIconMargin);
        mMenuTopDivider = a.getDrawable(R.styleable.MenuItem_menuTopDivider);
        mMenuBottomDivider = a.getDrawable(R.styleable.MenuItem_menuBottomDivider);
        mTopDividerHeight = a.getDimension(R.styleable.MenuItem_menuTopDividerHeight, mTopDividerHeight);
        mBottomDividerHeight = a.getDimension(R.styleable.MenuItem_menuBottomDividerHeight, mBottomDividerHeight);
        mMenuItemDivider = a.getDrawable(R.styleable.MenuItem_menuItemDivider);
        mMenuItemDividerHeight = a.getDimension(R.styleable.MenuItem_menuBottomDividerHeight, mMenuItemDividerHeight);
        mMenuRightTextDrawable = a.getDrawable(R.styleable.MenuItem_menuRightTextDrawable);
        mMenuRightTextMarginLeft = a.getDimension(R.styleable.MenuItem_menuRightTextMarginLeft, mMenuRightTextMarginLeft);
        mMenuRightTextMarginTop = a.getDimension(R.styleable.MenuItem_menuRightTextMarginTop, mMenuRightTextMarginTop);
        mMenuRightTextMarginRight = a.getDimension(R.styleable.MenuItem_menuRightTextMarginRight, mMenuRightTextMarginRight);
        mMenuRightTextMarginBottom = a.getDimension(R.styleable.MenuItem_menuRightTextMarginBottom, mMenuRightTextMarginBottom);

        mContainerPaddingLeft = a.getDimension(R.styleable.MenuItem_containerPaddingLeft, mContainerPaddingLeft);
        mContainerPaddingTop = a.getDimension(R.styleable.MenuItem_containerPaddingTop, mContainerPaddingTop);
        mContainerPaddingRight = a.getDimension(R.styleable.MenuItem_containerPaddingRight, mContainerPaddingRight);
        mContainerPaddingBottom = a.getDimension(R.styleable.MenuItem_containerPaddingBottom, mContainerPaddingBottom);
        mMenuRightTextDrawablePadding = a.getDimension(R.styleable.MenuItem_menuRightTextDrawablePadding, mMenuRightTextDrawablePadding);
        a.recycle();
        initViews();
    }

    private void initViews() {
        //setId(generateViewId());
        setOrientation(LinearLayout.VERTICAL);
        mContainer = new RelativeLayout(getContext());
        mContainer.setId(generateViewId());
        LinearLayout.LayoutParams lpContainer = new LinearLayout
                .LayoutParams(LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mContainer.setPadding((int) mContainerPaddingLeft, (int) mContainerPaddingTop, (int) mContainerPaddingRight, (int) mContainerPaddingBottom);

        if (null != mMenuTopDivider) {
            mTopDivider = new View(getContext());
            mTopDivider.setId(generateViewId());
            setBackground(mTopDivider, mMenuTopDivider);
            LinearLayout.LayoutParams lpLongDivider = new LinearLayout
                    .LayoutParams(LayoutParams.MATCH_PARENT, (int) mTopDividerHeight);
            addView(mTopDivider, lpLongDivider);
        }

        if (null != mMenuIconRes) {
            // 菜单Icon
            mMenuIcon = new ImageView(getContext());
            RelativeLayout.LayoutParams lpMenuIcon = new RelativeLayout.LayoutParams((int) mMenuIconWidth, (int) mMenuIconHeight);
            lpMenuIcon.addRule(RelativeLayout.CENTER_VERTICAL);
            lpMenuIcon.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            mMenuIcon.setId(generateViewId());
            mMenuIcon.setImageDrawable(mMenuIconRes);
            lpMenuIcon.setMargins(0, 0, (int) mMenuIconMargin, 0);
            mContainer.addView(mMenuIcon, lpMenuIcon);
        }

        LinearLayout titleContainer = null;
        if (!TextUtils.isEmpty(mSubTitleText) && !TextUtils.isEmpty(mTitleText)) {
            // 标题和子标题同时存在,居中显示
            titleContainer = new LinearLayout(getContext());
            titleContainer.setId(generateViewId());
            titleContainer.setOrientation(LinearLayout.VERTICAL);
            mTitle = new TextView(getContext());
            mTitle.setText(mTitleText);
            mTitle.setTextColor(mTitleColor);
            mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,mTitleSize);
            LinearLayout.LayoutParams lpTitle = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            lpTitle.setMargins(0, 0, 0, (int) mMenuTitleMargin);
            titleContainer.addView(mTitle, lpTitle);

            mSubTitle = new TextView(getContext());
            mSubTitle.setText(mSubTitleText);
            mSubTitle.setTextColor(mSubTitleColor);
            mSubTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,mSubTitleSize);
            LinearLayout.LayoutParams lpSubTitle = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            titleContainer.addView(mSubTitle, lpSubTitle);

            RelativeLayout.LayoutParams lpTitleContainer = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT);
            lpTitleContainer.addRule(RelativeLayout.CENTER_VERTICAL);
            if (null != mMenuIcon) {
                // 如果存在Icon则标题居其右
                lpTitleContainer.addRule(RelativeLayout.RIGHT_OF, mMenuIcon.getId());
            }
            mContainer.addView(titleContainer, lpTitleContainer);
        } else if (!TextUtils.isEmpty(mTitleText)) {
            // 只有标题存在直接居中
            mTitle = new TextView(getContext());
            mTitle.setId(generateViewId());
            mTitle.setText(mTitleText);
            mTitle.setTextColor(mTitleColor);
            mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,mTitleSize);
            RelativeLayout.LayoutParams lpTitle = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            lpTitle.addRule(RelativeLayout.CENTER_VERTICAL);
            if (null != mMenuIcon) {
                // 如果存在Icon则标题居其右
                lpTitle.addRule(RelativeLayout.RIGHT_OF, mMenuIcon.getId());
            }
            mContainer.addView(mTitle, lpTitle);
        }

        if (null != mMenuItemDivider) {
            mItemDivider = new View(getContext());
            mItemDivider.setId(generateViewId());
            setBackground(mItemDivider, mMenuItemDivider);
            RelativeLayout.LayoutParams lpItemDivider = new RelativeLayout
                    .LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, (int) mMenuItemDividerHeight);
            if (null != titleContainer) {
                lpItemDivider.addRule(RelativeLayout.ALIGN_LEFT, titleContainer.getId());
            } else if (null != mTitle) {
                lpItemDivider.addRule(RelativeLayout.ALIGN_LEFT, mTitle.getId());
            }
            lpItemDivider.addRule(RelativeLayout.ALIGN_BASELINE, getId());
            mContainer.addView(mItemDivider, lpItemDivider);
        }

        if (null != mMenuToggleButtonRes) {
            // 开关存在直接显示
            mToggleButton = new ToggleButton(getContext());
            mToggleButton.setButtonDrawable(mMenuToggleButtonRes);
            mToggleButton.setBackgroundColor(0x00000000);
            mToggleButton.setTextOff(null);
            mToggleButton.setTextOn(null);
            mToggleButton.setText(null);
            mToggleButton.setChecked(mToggleButtonState);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            mContainer.addView(mToggleButton, lp);
        } else {
            // 开关不存在,判断显示arrow和右侧文字
            if (null != mRightArrowRes) {
                mRightArrow = new ImageView(getContext());
                mRightArrow.setId(generateViewId());
                mRightArrow.setImageDrawable(mRightArrowRes);
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(40, 40);
                lp.addRule(RelativeLayout.CENTER_VERTICAL);
                lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                mContainer.addView(mRightArrow, lp);
            }

            if (null != mRightText) {
                mRightTextView = new TextView(getContext());
                mRightTextView.setId(generateViewId());
                mRightTextView.setTextColor(mRightTextColor);
                mRightTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mRightTextSize);
                mRightTextView.setAutoLinkMask(mRightTextAutoLink);
                mRightTextView.setText(mRightText);
                if (null != mMenuRightTextDrawable) {
                    mMenuRightTextDrawable.setBounds(0, 0, mMenuRightTextDrawable.getMinimumWidth(), mMenuRightTextDrawable.getMinimumHeight());
                    mRightTextView.setCompoundDrawables(mMenuRightTextDrawable, null, null, null);
                    mRightTextView.setCompoundDrawablePadding((int) mMenuRightTextDrawablePadding);
                }
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.CENTER_VERTICAL);
                lp.setMargins((int) mMenuRightTextMarginLeft, (int) mMenuRightTextMarginTop, (int) mMenuRightTextMarginRight, (int) mMenuRightTextMarginBottom);
                if (null != mRightArrow) {
                    lp.addRule(RelativeLayout.LEFT_OF, mRightArrow.getId());
                }else{
                    lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                }
                mContainer.addView(mRightTextView, lp);
            }
        }

        addView(mContainer, lpContainer);

        if (null != mMenuBottomDivider) {
            mBottomDivider = new View(getContext());
            mBottomDivider.setId(generateViewId());
            setBackground(mBottomDivider, mMenuBottomDivider);
            LinearLayout.LayoutParams lpShortDivider = new LinearLayout
                    .LayoutParams(LayoutParams.MATCH_PARENT, (int) mBottomDividerHeight);
            addView(mBottomDivider, lpShortDivider);
        }

        //setOnClickListener(mViewOnClickListener);
    }

//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        return true;
//    }

//    public void setMenuItemOnClickListener(MenuItemOnClickListener listener) {
//        if (null != listener) {
//            mMenuItemOnClickListener = listener;
//        }
//    }

    public void setRightText(String rightText) {
        if (null != mRightTextView) {
            mRightTextView.setText(rightText);
        }
    }
    public TextView getTitleText() {
        return mTitle;
    }
    public TextView getRightText() {
        return mRightTextView;
    }
    public ToggleButton getRightToggleButton(){
        return mToggleButton;
    }
    public void setChecked(boolean state) {
        if (null != mToggleButton) {
            mToggleButton.setChecked(state);
        }
    }

    public interface MenuItemOnClickListener {
        void onClick(View view);
    }

    private void setBackground(View view, Drawable drawable) {
        if (Build.VERSION.SDK_INT >= 16) {
            view.setBackground(drawable);
        } else {
            view.setBackgroundDrawable(drawable);
        }
    }

    public static int generateViewId() {
        for (; ; ) {
            final int result = sNextGeneratedId.get();
            // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
            int newValue = result + 1;
            if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
            if (sNextGeneratedId.compareAndSet(result, newValue)) {
                return result;
            }
        }
    }
}
